package com.android.people;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SDExplorer extends ListActivity {
    private static final String TAG = "SDExplorer";
    private static final boolean DEBUG = true;
    
    private static final String SD_ROOT = "/sdcard/";
    
    private String mCurrentPreviewDir = "/sdcard/";
    private ImageButton mSDButton;
    private Map<View, String> mBtDirMap;
    private List<String> mFileList;
    private ViewGroup mButtonVG;
    
    private Menu mMenu;
    
    private View.OnClickListener mClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if (mBtDirMap.containsKey(v) == true) {
                mCurrentPreviewDir = mBtDirMap.get(v);
                getListView().setAdapter(new FileListAdapter(mCurrentPreviewDir, SDExplorer.this));
                for (View vSaved : mBtDirMap.keySet()) {
                    String path = mBtDirMap.get(vSaved);
                    if (path.length() > mCurrentPreviewDir.length()) {
                        mButtonVG.removeView(vSaved);
                    }
                }
            }
        }
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.setContentView(R.layout.sdexplorer);
        mBtDirMap = new HashMap<View, String>();
        mFileList = new ArrayList<String>();
        mSDButton = (ImageButton) this.findViewById(R.id.sd_dir_button);
        mSDButton.setImageResource(R.drawable.icon_sdcard);
        mButtonVG = (ViewGroup) this.findViewById(R.id.dir_list);
        mSDButton.setOnClickListener(mClickListener);
        mBtDirMap.put(mSDButton, SD_ROOT);
    }
    
    @Override
    public void onStart() {
        super.onStart();
        this.getListView().setAdapter(new FileListAdapter(mCurrentPreviewDir, this));
    }
    
    @Override
    public void onStop() {
        super.onStop();
    }
  
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String filename = mFileList.get(position);
        File file = new File(mCurrentPreviewDir + filename);
        if (file.exists() == true && file.isDirectory() == true) {
            mCurrentPreviewDir = mCurrentPreviewDir + filename + "/";
            this.getListView().setAdapter(new FileListAdapter(mCurrentPreviewDir, this));
            Button bt = new Button(this);
            bt.setText(filename);
            bt.setHeight(48);
            bt.setOnClickListener(mClickListener);
            mButtonVG.addView(bt);
            mBtDirMap.put(bt, mCurrentPreviewDir);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Hold on to this
        mMenu = menu;
        
        // Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.cancel:
            finish();
            break;
        case R.id.ok:
            Intent resultValue = new Intent();
            resultValue.putExtra(PhotoWidgetConfig.BROWSE_RESULT, mCurrentPreviewDir);
            setResult(Activity.RESULT_OK, resultValue);
            finish();
            break;
        }
        return true;
    }
    
    private class FileListAdapter extends BaseAdapter {
        private String mDirPath;
        private Context mContext;
        
        public FileListAdapter(String path, Context context) {
            mDirPath = path;
            mContext = context;
            File file = new File(mDirPath);
            mFileList.clear();
            if (file.isDirectory() == true && file.exists() == true) {
                String[] files = file.list();
                if (files != null) {
                    for (String filename : files) {
                        mFileList.add(filename);
                    }
                }
            }
        }
        
        public int getCount() {
            return mFileList.size();
        }
        
        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            String filename = mFileList.get(position);
            File file = new File(mDirPath + filename);
            if (file.exists() == true) {
                ViewGroup layout = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
                if (layout != null) {
                    ImageView icon = (ImageView) layout.findViewById(R.id.icon);
                    if (file.isDirectory() == true) {
                        icon.setImageResource(R.drawable.ic_launcher_folder);
                    } else {
                        icon.setImageResource(R.drawable.icon_file);
                    }
                    TextView tv = (TextView) layout.findViewById(R.id.name);
                    tv.setText(filename);
                    return layout;
                }
            }
            return null;
        }
        
    }
    
    private static void LOGD(String msg) {
        if (DEBUG) {
            Log.d(TAG, msg);
        }
    }
}
